package com.can.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.can.constant.SystemConstants;
import com.can.convert.CommentConvert;
import com.can.domain.ResponseResult;
import com.can.domain.entity.Comment;
import com.can.domain.entity.User;
import com.can.domain.vo.CommentVO;
import com.can.domain.vo.PageVO;
import com.can.enums.AppHttpCodeEnum;
import com.can.exception.SystemException;
import com.can.mapper.CommentMapper;
import com.can.service.CommentService;
import com.can.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment) 服务实现类
 *
 * @author makejava
 * @since 2023-05-08 13:40:04
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //对articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType), Comment::getArticleId, articleId);
        //根评论 rootId为-1
        queryWrapper.eq(Comment::getRootId, -1);
        //评论类型
        queryWrapper.eq(Comment::getType, commentType);
        //分页查询
        Page<Comment> page = new Page(pageNum, pageSize);
        page(page, queryWrapper);

        List<CommentVO> commentVOList = toCommentVOList(page.getRecords());

        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVO commentVO : commentVOList) {
            //查询对应的子评论
            List<CommentVO> children = getChildren(commentVO.getId());
            //赋值
            commentVO.setChildren(children);
        }

        return ResponseResult.okResult(new PageVO(commentVOList, page.getTotal()));
    }

    // 根据根评论的id查询所对应的子评论的集合
    private List<CommentVO> getChildren(Long id) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId, id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);

        List<CommentVO> commentVOs = toCommentVOList(comments);
        return commentVOs;
    }

    private List<CommentVO> toCommentVOList(List<Comment> list) {
        List<CommentVO> CommentVOS = CommentConvert.INSTANCE.convertList(list);
        //遍历vo集合
        for (CommentVO commentVO : CommentVOS) {
            //通过creatyBy查询用户的昵称,头像
            User user = userService.getById(commentVO.getCreateBy());
            commentVO.setUsername(user.getNickName());
            commentVO.setAvatar(user.getAvatar());
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if (commentVO.getToCommentUserId() != -1) {
                User toCommentUser = userService.getById(commentVO.getToCommentUserId());
                commentVO.setToCommentUserName(toCommentUser.getNickName());
            }
        }
        return CommentVOS;
    }
}

